package com.atguigu.ucenterservice.service.impl;

import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.LoginVo;
import com.atguigu.ucenterservice.entity.vo.RegisterVo;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.atguigu.ucenterservice.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-30
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
        //1取出所有参数
        String nickname = registerVo.getNickname();//昵称
        String mobile = registerVo.getMobile();//电话号
        String password = registerVo.getPassword();//密码
        String code = registerVo.getCode();//验证码
        //2验空
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "注册信息不能为空");
        }
        //3验证手机号是否重复
        QueryWrapper<UcenterMember> memberWrapper  = new QueryWrapper<>();
        memberWrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(memberWrapper);//不用返回具体信息 知道数就OK
        if (count > 0) {
            throw new GuliException(20001,"手机号不能重复注册");
        }
        //4根据手机号从redis获取验证进行验证
        String redisCode  = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001,"验证码有误");
        }
        //5密码进行md5加密
        String passwordMD5  = MD5.encrypt(password);
        //6补全信息，存入数据库
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo, ucenterMember);
        ucenterMember.setPassword(passwordMD5);
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://guli-file-190513.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");
        baseMapper.insert(ucenterMember);
    }

    //用户登录
    @Override
    public String login(LoginVo loginVo) {
        //1 取出参数
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //2 验空
        if(StringUtils.isEmpty(mobile)
                ||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"用户名或密码有误");
        }
        //3 根据手机号取出用户信息，信息为空返回失败
        QueryWrapper<UcenterMember> memberWrapper = new QueryWrapper<>();
        memberWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(memberWrapper);
        if(member==null){
            throw new GuliException(20001,"用户名或密码有误");
        }
        //4 加密密码，进行校验
        String passwordMD5 = MD5.encrypt(password);
        if(!passwordMD5.equals(member.getPassword())){
            throw new GuliException(20001,"用户名或密码有误");
        }
        //5 生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    //根据日期统计用户注册信息,远程调用
    @Override
    public Integer countRegister(String day) {
        Integer count = baseMapper.countRegister(day);
        return count;
    }
}
